<?php
namespace Agent\Controller;
use Think\Controller;
use Think\Image;
class EfficiencyController extends PublicController {


    function _initialize() {
        parent::_initialize ();
    }
    //商务列表
    public function index(){
        $admin_id = $_SESSION["agentInfo"]["admin_id"];
        if(empty($admin_id)){
            $this->error('信息错误');exit;
        }
        $search = I('search');
        $seo = I('seo');
        if($search){
            $where['u.user_name|w.keyword|w.site'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        $seo = I('seo');
        if($seo){
            $where['w.seo'] = array('eq',$seo);
            $this->assign("seo", $seo);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $where['w.admin_id'] = $admin_id;
        $where['w.status'] = 1;
        $logTime =  strtotime(date('Y-m-d',strtotime('-1 day')));
        // var_dump($logTime);exit;
        $where['w.logtime'] = array('gt',$logTime);
        $header = C('header')['efficiency'];
        $count = M('worlddetails')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('w.id,u.user_name,w.site,w.keyword,w.seo,w.logtime,w.amount,w.price,w.standard,w.standard*w.price,u.status')
                    ->where($where)
                    ->count();
       


        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出
        $list = M('worlddetails')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('w.id,u.user_name,w.site,w.keyword,w.seo,w.logtime,w.amount,w.price,w.standard,w.standard*w.price,u.status,w.k_id as details')
                    ->where($where)
                    ->order('w.id desc')
                    ->limit($Page->firstRow . ',' . $Page->listRows)
                    ->select();
        $status = ['正常','欠费'];
        $seo = [1=>'百度PC','百度移动','360PC','360移动','搜狗PC','搜狗移动'];
        $seo_site = [
            '百度PC' => 'https://www.baidu.com/s?&wd=',
            '百度移动' => 'http://wap.baidu.com/s?word=',
            '360PC' => 'https://www.so.com/s?q=',
            '360移动' => 'https://m.so.com/s?q=',
            '搜狗PC' => 'http://www.sogou.com/sogou?query=',
            '搜狗移动' => 'https://m.sogou.com/web/searchList.jsp?keyword=',
        ];
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
            $list[$k]['seo'] = $seo[$v['seo']];
            $list[$k]['keyword'] =  '<a href="'.$seo_site[$seo[$v['seo']]].$v['keyword'].'" target="_blank">'.$v['keyword'].'</a>';
            $list[$k]['site'] =  '<a href="http://'.$v['site'].'" target="_blank">'.$v['site'].'</a>';
            $list[$k]['status'] = $status[$v['status']];
            $list[$k]['details'] = "<a href='#' class='details' k_id='{$v['details']}' seo='{$v['seo']}'>查看详情</a>";
           
        }

        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Efficiency');
        layout('Layout/twolayout');
        $this->display();
    }



    public function efficiencyday(){
        $admin_id = $_SESSION["agentInfo"]["admin_id"];
        if(empty($admin_id)){
            $this->error('信息错误');exit;
        }
        $where['w.admin_id'] = $admin_id;
        $where['w.status'] = 1;
        $datalist = [];
        if(IS_POST){
            $datalist = I('post.');
            S('efficiencydayu',$datalist,600);
        }elseif(I('p') && S('efficiencydayu')){
            $datalist = S('efficiencydayu');
        }elseif(I('excel') && S('efficiencydayu')){
            $datalist = S('efficiencydayu');
        }
        if($datalist['search']){
            $where['u.user_name|w.keyword'] = array('like',"%$search%");
            $this->assign("search", $datalist['search']);
        }

        if($datalist['seo']){
            $where['w.seo'] = array('eq',$datalist['seo']);
            $this->assign("seo", $datalist['seo']);
        }

        if($datalist['startdate']){
            $where['w.updatelogtime'] = array('EGT',$datalist['startdate']);
            $this->assign("startdate", $datalist['startdate']);
        }

        if($datalist['enddate']){
            $where['w.updatelogtime'] = array('ELT',$datalist['enddate']);
            $this->assign("enddate", $datalist['enddate']);
        }

        if(empty($datalist['startdate']) && empty($datalist['enddate'])){
            $where['w.updatelogtime'] = array('EGT',strtotime(date('Y-m-d',time())));
        }


        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }

        $where['w.status'] = 1;
        $where['u.status'] = 0;
        $where['w.last_rank'] = array('gt',0);
        $where['w.last_rank'] = array('lt',10);
        // $logTime =  strtotime(date('Y-m-d',strtotime('-1 day')));
        // var_dump($logTime);exit;
        $logTime =  strtotime(date('Y-m-d',time()));
        $where['w.updatelogtime'] = array('gt',$logTime);
        $header = C('header')['wordDay'];
        $count = M('worlddetails_log')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('w.id,u.user_name,w.site,w.keyword,w.seo,w.updatelogtime,w.amount,w.price,w.standard,w.standard*w.price,u.status')
                    ->where($where)
                    ->count();


        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出





        $list = M('worlddetails_log')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('w.id,u.user_name,w.site,w.keyword,w.seo,w.updatelogtime,w.td_rank,w.last_rank,w.amount,w.price,w.standard,w.standard*w.price,u.status')
                    ->where($where)
                    ->order('w.id desc')
                    ->limit($Page->firstRow . ',' . $Page->listRows)
                    ->select();
        $status = ['进行中','欠费'];
        $seo = [1=>'百度PC','百度移动','360PC','360移动','搜狗PC','搜狗移动'];
        $seo_site = [
            '百度PC' => 'https://www.baidu.com/s?&wd=',
            '百度移动' => 'http://wap.baidu.com/s?word=',
            '360PC' => 'https://www.so.com/s?q=',
            '360移动' => 'https://m.so.com/s?q=',
            '搜狗PC' => 'http://www.sogou.com/sogou?query=',
            '搜狗移动' => 'https://m.sogou.com/web/searchList.jsp?keyword=',
        ];              
        foreach ($list as $k => $v) {
            $list[$k]['keyword'] =  '<a href="'.$seo_site[$seo[$v['seo']]].$v['keyword'].'" target="_blank">'.$v['keyword'].'</a>';
            $list[$k]['site'] =  '<a href="http://'.$v['site'].'" target="_blank">'.$v['site'].'</a>';
            $list[$k]['updatelogtime'] = date('Y-m-d H:i:s',$v['updatelogtime']);
            $list[$k]['seo'] = $seo[$v['seo']];
            if($v['td_rank'] == 51){
                $list[$k]['td_rank'] = '50+';
            }
            if($v['last_rank'] == 51){
                $list[$k]['last_rank'] = '50+';
            }
            $list[$k]['status'] = $status[$v['status']];  
        }


        if(I('excel')){
            getExcel('上词数据-', $header, $list);
            exit;
        }
        

        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Efficiency');
        layout('Layout/twolayout');
        $this->display();
    }


    //代理商上词数据
    public function efficiencystatistical(){
        $search = I('search');
        if($search){
            $where['u.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        // $header = C('header')['wordDay'];
        $header = [];
        $count = M('word_statistics')->group('admin_id')->count();

        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出





        $list = M('word_statistics')
                    ->alias("w")
                    ->join("left join pt_admin as u on w.admin_id=u.admin_id")
                    ->field('u.user_name,sum(w.bd_keywords) as bdk,sum(w.bd_standard) as bds,w.admin_id as bdlv,sum(w.bdm_keywords) as bdmk,sum(w.bdm_standard) as bdms,w.bd_keywords as bdmlv,sum(w.360_keywords) as 360k,sum(w.360_standard) as 360s,w.bd_keywords as 360lv,sum(w.360m_keywords) as 360mk,sum(w.360m_standard) as 360ms,w.bd_keywords as 360mlv,sum(w.sogo_keywords) as sogok,sum(w.sogo_standard) as sogos,w.bd_keywords as sogolv,sum(w.sogom_keywords) as sogomk,sum(w.sogom_standard) as sogoms,w.bd_keywords as sogomlv,sum(w.keywords) as k,sum(w.standard) as s,w.bd_keywords as lv')
                    ->group('w.admin_id')
                    ->limit($Page->firstRow . ',' . $Page->listRows)
                    ->select();
        foreach ($list as $k => $v) {
                $list[$k]['user_name'] = "<a href='/Admin/efficiency/efficiencystatisticalu?admin_id={$v['bdlv']}'>{$v['user_name']}</a>";
                $list[$k]['bdlv'] = round(($v['bds']/$v['bdk'])*100,2).'％';
                $list[$k]['bdmlv'] = round(($v['bdms']/$v['bdmk'])*100,2).'％';
                $list[$k]['360lv'] = round(($v['360s']/$v['360k'])*100,2).'％';
                $list[$k]['360mlv'] = round(($v['360ms']/$v['360mk'])*100,2).'％';
                $list[$k]['sogolv'] = round(($v['sogos']/$v['sogok'])*100,2).'％';
                $list[$k]['sogomlv'] = round(($v['sogoms']/$v['sogomk'])*100,2).'％';
                $list[$k]['lv'] = round(($v['s']/$v['k'])*100,2).'％';
             }     
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Efficiency');
        layout('Layout/twolayout');
        $this->display();
    }



    //指定代理商下的客户的上词数据
    public function efficiencystatisticalu(){
        $admin_id = $_SESSION["agentInfo"]["admin_id"];
        if(empty($admin_id)){
            $this->error('信息错误');exit;
        }
        
        $search = I('search');
        if($search){
            $where['u.user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        // $header = C('header')['wordDay'];
        $header = [];
        $where['w.admin_id'] = $admin_id;
        $count = M('word_statistics')
                ->alias("w")
                ->join("left join pt_user as u on w.admin_id=u.admin_id")
                ->where($where)
                ->group('w.user_id')
                ->count();

        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出



            $wheres['w.admin_id'] = $admin_id;
            $list = M('word_statistics')
                    ->alias("w")
                    ->join("left join pt_user as u on w.user_id=u.user_id")
                    ->field('u.user_name,w.bd_keywords as bdk,w.bd_standard as bds,w.user_id as bdlv,w.bdm_keywords as bdmk,w.bdm_standard as bdms,w.bd_keywords as bdmlv,w.360_keywords as 360k,w.360_standard as 360s,w.bd_keywords as 360lv,w.360m_keywords as 360mk,w.360m_standard as 360ms,w.bd_keywords as 360mlv,w.sogo_keywords as sogok,w.sogo_standard as sogos,w.bd_keywords as sogolv,w.sogom_keywords as sogomk,w.sogom_standard as sogoms,w.bd_keywords as sogomlv,w.keywords as k,w.standard as s,w.bd_keywords as lv')
                    ->group('w.user_id')
                    ->where($where)
                    ->limit($Page->firstRow . ',' . $Page->listRows)
                    ->select();
        foreach ($list as $k => $v) {
                $list[$k]['bdlv'] = round(($v['bds']/$v['bdk'])*100,2).'％';
                $list[$k]['bdmlv'] = round(($v['bdms']/$v['bdmk'])*100,2).'％';
                $list[$k]['360lv'] = round(($v['360s']/$v['360k'])*100,2).'％';
                $list[$k]['360mlv'] = round(($v['360ms']/$v['360mk'])*100,2).'％';
                $list[$k]['sogolv'] = round(($v['sogos']/$v['sogok'])*100,2).'％';
                $list[$k]['sogomlv'] = round(($v['sogoms']/$v['sogomk'])*100,2).'％';
                $list[$k]['lv'] = round(($v['s']/$v['k'])*100,2).'％';
             }


        // $list = array(
        //     array('邦伲德','42','38',round(28/42*100,2).'％','42','36',round(36/42*100,2).'％','42','29',round(29/42*100,2).'％','42','39',round(39/42*100,2).'％','42','36',round(36/42*100,2).'％','42','29',round(29/42*100,2).'％','228','205',round(205/228*100,2).'％'),
        //     array('上海包答答','51','38',round(38/51*100,2).'％','51','36',round(36/51*100,2).'％','51','39',round(39/51*100,2).'％','51','39',round(39/51*100,2).'％','51','36',round(36/51*100,2).'％','51','39',round(39/51*100,2).'％','366','342',round(342/366*100,2).'％'),
        //     array('拓顺','66','58',round(58/66*100,2).'％','66','56',round(56/66*100,2).'％','66','59',round(59/66*100,2).'％','66','59',round(59/66*100,2).'％','66','56',round(56/66*100,2).'％','66','58',round(58/66*100,2).'％','396','365',round(365/396*100,2).'％'),
        //     array('百馨','78','68',round(68/78*100,2).'％','78','66',round(66/78*100,2).'％','78','69',round(69/78*100,2).'％','78','66',round(66/78*100,2).'％','78','65',round(65/78*100,2).'％','78','69',round(69/78*100,2).'％','468','429',round(429/468*100,2).'％')
        // );     

        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Efficiency');
        layout('Layout/twolayout');
        $this->display();
    }




     public function edit(){
        $id = I('id');
        if(!$id){
            $this->ajaxreturn(array('state' => 0, 'data' => '信息错误'), 'json');
        }
        $where['id'] = $id;
        
        $list = M('business')->where($where)->field('id,admin_id,user_name,phone,email,qq')->find();
        if(empty($list)){
            $this->ajaxreturn(array('state' => 0, 'data' => '不存在该用户'), 'json');
        }else{
            $value = $list['admin_id'];
            unset($list['admin_id']);
            $this->ajaxreturn(array('state' => 1, 'data' => $list,'value'=>$value), 'json');
        }
    }


    //商务编辑
    public function handle(){
        $res = I('');
        if($res['act'] == 'add'){
           
            if(!$res['user_name'] ||  !$res['phone'] || !$res['qq'] || !$res['email'] || !$res['admin_id']){
                $this->error('请填写完整信息');
            }
           
            $res['add_time'] = time();
            $res['update_time'] = time();
            $res['type'] = 1;
            $list = M('business')->add($res);
            if($list){
                $this->success("添加成功", U("Business/index"));exit;
            }else{
                $this->error("添加失败");exit;
            }

            $this->display();
        }elseif($res['act'] == 'edit'){
            $data = [];
            $update['user_name'] = $res['user_name'];
            $update['phone'] = $res['phone'];
            $update['email'] = $res['email'];
            $update['admin_id'] = $res['admin_id'];
            $update['qq'] = $res['qq'];
            $update['update_time'] = time();
            $where['id'] = $res['id'];
            $list = M('business')->where($where)->save($update);
            if($list){
                $this->success("更新成功", U("Business/index"));exit;
            }else{
                $this->error("更新失败");exit;
            }
        }elseif($res['act'] == 'del'){
            $where['id'] = $res['id'];
            $list = M('business')->where($where)->delete($res);
            if($list){
                $this->success("删除成功", U("Business/index"));exit;
            }else{
                $this->error("删除失败");exit;
            }
        }
       
        
    }




    public function details(){
        $k_id = I('k_id');
        $seo = I('seo');
        if(!$k_id || !$seo){
            $this->ajaxreturn(array('state' => 2, 'data' => '信息错误'), 'json');
        }

        $where['c.keyword_id'] = $k_id;
        $where['c.seo'] = $seo;
        $where['c.type'] = 2;
        $list = M('ucharge')
                ->alias("c")
                ->join("left join pt_user as u on u.user_id=c.uid")
                ->join("left join pt_keyword as k on k.id=c.keyword_id")
                ->field('c.charge_id,u.user_name,k.Keyword,c.logtime,c.desc,c.amount,c.desc as note')
                ->order('c.charge_id desc')
                ->where($where)
                ->select();
        if($list){
            $n = 1;
            foreach ($list as $k => $v) {
                $list[$k]['charge_id'] = $n;
                $list[$k]['logtime'] = date("Y-m-d H:i:s",$v['logtime']);
                $list[$k]['desc'] = '排名消费';
                $list[$k]['note'] = $v['log_date'].'任务达标扣费';
                $n++;
            }
            $this->ajaxreturn(array('state' => 1, 'data' => $list), 'json');exit;
        }else{
            $this->ajaxreturn(array('state' => 2, 'data' => '数据为空'), 'json');exit;
        }
    }

}